package com.lancoo.znbkxx.teacher.ui.fragment

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager.widget.ViewPager
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.utils.CardTransformer
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.lancoo.znbkxx.teacher.viewmodel.HardSolutionViewModel
import kotlinx.android.synthetic.main.view_select_answer_skill.view.*

/**
 * Created by wealive on 2021/3/12.
 *
 *
 */
class SelectAnswerSkillDialog(val listener: View.OnClickListener) : AppCompatDialogFragment(),
        SelectAnswerSKillFragment.onChangeListener {
    private val viewModel by lazy {
        ViewModelProvider(requireActivity()).get(HardSolutionViewModel::class.java)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        return inflater.inflate(R.layout.view_select_answer_skill, container, false)
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        with(view) {
            vp.offscreenPageLimit = viewModel.answerSkillList.value!!.size
            vp.adapter = object : FragmentStatePagerAdapter(childFragmentManager,
                    BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
                override fun getItem(position: Int): Fragment {
                    return SelectAnswerSKillFragment(this@SelectAnswerSkillDialog).apply {
                        arguments = bundleOf(Pair("position", position))
                    }
                }

                override fun getCount(): Int {
                    return viewModel.answerSkillList.value!!.size
                }
            }
            tv_page_num.text = "1/${viewModel.answerSkillList.value!!.size}"
            vp.setPageTransformer(true, CardTransformer(context))
            vp.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
                override fun onPageSelected(position: Int) {
                    tv_page_num.text = "${vp.currentItem + 1}/${viewModel.answerSkillList.value!!.size}"
                }
            })
            iv_close.setOnClickListener(listener)
        }
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        val param = this.dialog?.window?.attributes
        param?.width = WindowManager.LayoutParams.MATCH_PARENT
        param?.height = DensityUtils.dp2px(context, 380f)
        dialog?.setCanceledOnTouchOutside(false)
        dialog?.window?.attributes = param
    }


    override fun add(position: Int, isAdd: Boolean) {
        viewModel.answerSkillList.value!![requireView().vp.currentItem].AnswerSkillDataList[0].isSelect = isAdd
    }

    fun getTotalAddNum(): Int {
        var num = 0
        viewModel.answerSkillList.value!!.forEach {
            if (it.AnswerSkillDataList[0].isSelect) {
                num++
            }
        }
        return num
    }
}